2.1 Matplotlib - Wykresy i krzywe

Prosty wykres krzywej


In [9]:
%matplotlib inline

In [10]:
import numpy as np
import matplotlib.pyplot as plt

## initialize the axes
fig = plt.figure()
ax = fig.add_subplot(111)

## format axes
ax.set_ylabel('volts')
ax.set_title('a sine wave')

t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2*np.pi*t)
line, = ax.plot(t, s, color='blue', lw=2)


Style wykresów


In [11]:
import numpy as np
import matplotlib.pyplot as plt

## initialize the figure
fig = plt.figure(figsize=(15,10))

## the data
t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2*np.pi*t)

## the top axes
ax1 = fig.add_subplot(3,1,1)
ax1.set_ylabel('volts')
ax1.set_title('a sine wave')

line1 = ax1.plot(t, s+5.0, color='blue', lw=2)
line2 = ax1.plot(t, s+2.5, color='red', lw=2)
line3 = ax1.plot(t, s, color='orange', lw=2)

## the middle axes
ax2 = fig.add_subplot(3,1,2)
ax2.set_ylabel('volts')
ax2.set_title('a sine wave')

line1 = ax2.plot(t, s+5.0, color='black', lw=2,linestyle="--")
line2 = ax2.plot(t, s+2.5, color='black', lw=2,linestyle="-.")
line3 = ax2.plot(t, s, color='#000000', lw=2,linestyle=":")

## the thrid axes
ax3 = fig.add_subplot(3,1,3)
ax3.set_ylabel('volts')
ax3.set_title('a sine wave')

line1 = ax3.plot(t,s+5.0, color='blue', marker="+")
line2 = ax3.plot(t,s+2.5, color='red', marker="o")
line3 = ax3.plot(t,s, color='orange', marker="^")

## adjust the space between plots
plt.subplots_adjust(wspace=0.2,hspace=.4)


Wykresy 3D


In [15]:
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(15,10))
ax =  fig.add_subplot(111, projection='3d')

theta = np.linspace(-4 * np.pi, 4 * np.pi, 50)
z = np.linspace(-2, 2, 50)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)

ax.plot(x, y, z, label='parametric curve')
ax.legend()

plt.show()


Powierzchnie 3D


In [ ]:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(15,10))
ax = fig.add_subplot(111, projection='3d')

X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet,
        linewidth=0, antialiased=True)

ax.set_zlim(-1.01, 1.01)

plt.show()

Zadania 2.1

  1. Proszę umieścić wykresy funkcji $f(x) = \frac{e^x}{e^x + 1}$ oraz $f(x)=\tanh(x)$ w przedziale $[-10, 10]$ na jednym obiekcie matplotlib, proszę dodać legendę. Wykorzystać różne style i kolory.
  2. Proszę stworzyć wykres trójwymiarowy funkcji $f(x,y) = -(x^3 + y^2)$ podobny do poniższego: